//
//  QLNetWorkCenter.m
//  Advertisement
//
//  Created by mac on 16/5/3.
//  Copyright © 2016年 Appcoda. All rights reserved.
//

#import "QLNetWorkCenter.h" 
#import "AFNetworking.h"
@interface QLNetWorkCenter()
QL_Nonatomic_Copy QLNetWorkCenterSuccessBlock succBlock;
QL_Nonatomic_Copy QLNetWorkCenterFailedBlock failBlock;
QL_Nonatomic_Strong AFHTTPSessionManager * afManager;




@end
@implementation QLNetWorkCenter

-(AFHTTPSessionManager *)afManager
{
    if (!_afManager) {
        _afManager = [[AFHTTPSessionManager alloc]init];
    }
    return _afManager;
}

- (NSString *)interfaceChain
{return @"";}

- (NSString *)baseServerURL
{
    return @"http://esapp.cceschool.com/handler/";
}

- (void)handleUrlStringBeforSendRequest{}


- (void)startRequest
{
    [self handleUrlStringBeforSendRequest];
    NSLog(@"handleUrlString is %@",self.netURLString);
    __weak __typeof(self)weekSelf = self;
    if (self.responseSerializer) {
        self.afManager.responseSerializer = self.responseSerializer;
    }else{
        self.afManager.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    [self.afManager GET:self.netURLString parameters:self.requestParameters success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"-----%@",responseObject);
        
        weekSelf.resultJSON = responseObject;
        weekSelf.resultDictionary = responseObject;
        [weekSelf handleFinishDataBeforInvoke:self];
        
        if (weekSelf.delegate && [weekSelf.delegate respondsToSelector:@selector(qlRequestFinished:)]) {
            [weekSelf.delegate qlRequestFinished:self];
        }
        if (weekSelf.succBlock)
        {
            weekSelf.succBlock(weekSelf);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        [weekSelf handleFailDataBeforInvoke:self];
//        weekSelf.errorInfo = error;
//        if (weekSelf.delegate && [weekSelf.delegate respondsToSelector:@selector(qlRequestFailed:)]) {
//            [weekSelf.delegate qlRequestFailed:self];
//        }
//        if (weekSelf.failBlock)
//        {
//            weekSelf.failBlock(self);
//        }
//        NSLog(@"[error][][]--%@",[weekSelf.errorInfo localizedDescription]);
    }];
}

- (void)startRequestByParameters
{
    [self joinBaseURLandInterface];
    [self startRequest];
}

- (NSString *)resultString
{
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.resultJSON options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}


- (NSString *)postUrl
{
    return [[NSString alloc]initWithFormat:@"%@%@",[self baseServerURL],[self interfaceChain]];
}

- (void)joinBaseURLandInterface
{
    self.netURLString = [[NSString alloc]initWithFormat:@"%@%@",[self baseServerURL],[self interfaceChain]];
//    __weak __strong 用来修饰变量
//    __unsafe_unretained __autoreleasing  也是用来修饰变量的
//    __unsafe_unretained;//声明一个弱应用，但是不会自动nil化，也就是说，如果所指向的内存区域被释放了，这个指针就是一个野指针了。
//    __autoreleasing;//用来修饰一个函数的参数，这个参数会在函数返回的时候被自动释放。
//    __weak;//声明了一个可以自动 nil 化的弱引用。
//    __strong;//是缺省的关键词。
}

- (void)heChengUrl
{
    [self joinBaseURLandInterface];
    NSMutableString *urlStirng = [[NSMutableString alloc]initWithString:self.netURLString];
    [urlStirng appendString:@"?"];
    __weak NSMutableString *weekUrlStirng = urlStirng;
    if (![self.requestParameters isKindOfClass:[NSMutableDictionary class]]) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.requestParameters];
        self.requestParameters = dic;
    }
    [self.requestParameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
//        [weekUrlStirng appendFormat:@"%@=%@&",[key description],[[obj description]URLEncodedString]];
    }];
    self.netURLString = urlStirng;
}

- (void)startByPramsWithSuccBlock:(QLNetWorkCenterSuccessBlock)aSuccBlock withFailedBlock:(QLNetWorkCenterFailedBlock)aFailedBlock
{
    self.succBlock = aSuccBlock;
    self.failBlock = aFailedBlock;
    [self startRequestByParameters];
}
- (void)handleFinishDataBeforInvoke:(QLNetWorkCenter *)request{}

@end
